package edu.bu.grocerylist.mock;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import edu.bu.grocerylist.dao.IGroceryListItemDao;
import edu.bu.grocerylist.model.Category;
import edu.bu.grocerylist.model.GroceryList;
import edu.bu.grocerylist.model.GroceryListItem;
import edu.bu.grocerylist.model.Item;

/**
 * Test class for GroceryListITemDao
 * @author donlanp
 *
 */
public class MockGroceryListItemDao implements IGroceryListItemDao {
	
	private List<GroceryListItem> listItems = new ArrayList<GroceryListItem>();
	
	private List<Category> categories = new ArrayList<Category>();
	
	private List<Item> items = new ArrayList<Item>();

	
	public MockGroceryListItemDao(){
		for(int i = 0; i < 10; i++){
			GroceryListItem item = new GroceryListItem();
			item.setCategory("cat" + i);
			item.setCategoryDescription("catdes" + i);
			item.setCategoryId(i);
			item.setItemId(i);
			item.setListId(i);
			item.setMeasurement("meas" + i);
			item.setQuantity(i);
			item.setName("item" + i);
			item.setStatus(GroceryListItem.NOT_SHOPPED);
			
			Category c = new Category();
			c.setName(item.getCategory());
			c.setDescription(item.getCategoryDescription());
			c.setCategoryId(item.getCategoryId());
			categories.add(c);
			
			Item it = new Item();
			it.setItemId(item.getItemId());
			it.setItemName(item.getName());
			items.add(it);
			
			listItems.add(item);
		}
	}
	
	/* (non-Javadoc)
	 * @see edu.bu.grocerylist.dao.IGroceryListItemDao#insertGroceryListItem(edu.bu.grocerylist.model.GroceryListItem)
	 */
	public GroceryListItem insertGroceryListItem(GroceryListItem item) {
		listItems.add(item);
		return item;
	}

	/* (non-Javadoc)
	 * @see edu.bu.grocerylist.dao.IGroceryListItemDao#updateGroceryListItem(edu.bu.grocerylist.model.GroceryListItem)
	 */
	public GroceryListItem updateGroceryListItem(GroceryListItem item) {
		GroceryListItem toUpdate = getGroceryListItemByPrimaryKey(item);
		
		toUpdate.setCategory(item.getCategory());
		toUpdate.setCategoryDescription(item.getCategoryDescription());
		toUpdate.setMeasurement(item.getMeasurement());
		toUpdate.setItemId(item.getItemId());
		toUpdate.setName(item.getName());
		toUpdate.setQuantity(item.getQuantity());
		toUpdate.setStatus(item.getStatus());
		
		return item;
	}

	/* (non-Javadoc)
	 * @see edu.bu.grocerylist.dao.IGroceryListItemDao#deleteGroceryListItem(edu.bu.grocerylist.model.GroceryListItem)
	 */
	public boolean deleteGroceryListItem(GroceryListItem item) {
		Iterator<GroceryListItem> iter = listItems.iterator();
		while(iter.hasNext()){
			GroceryListItem cur = iter.next();
			if(cur.getItemId() == item.getItemId() && cur.getListId() == item.getListId() && cur.getCategoryId() == item.getCategoryId()){
				iter.remove();
				return true;
			}
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see edu.bu.grocerylist.dao.IGroceryListItemDao#getGroceryListItemsByListId(int)
	 */
	public List<GroceryListItem> getGroceryListItemsByListId(int listId) {
		List<GroceryListItem> found = new ArrayList<GroceryListItem>();
		for(GroceryListItem i : listItems){
			if(i.getListId() == listId){
				found.add(i);
			}
		}
		
		return found;
	}


	/* (non-Javadoc)
	 * @see edu.bu.grocerylist.dao.IGroceryListItemDao#saveNewCategory(edu.bu.grocerylist.model.Category)
	 */
	public Category saveNewCategory(Category category) {
		categories.add(category);
		category.setCategoryId(categories.size() - 1);
		return category;
	}

	/* (non-Javadoc)
	 * @see edu.bu.grocerylist.dao.IGroceryListItemDao#saveNewItem(edu.bu.grocerylist.model.Item)
	 */
	public Item saveNewItem(Item item) {
		items.add(item);
		item.setItemId(items.size() - 1);
		return item;
	}

	/* (non-Javadoc)
	 * @see edu.bu.grocerylist.dao.IGroceryListItemDao#doesItemExist(java.lang.String)
	 */
	public boolean doesItemExist(String itemName) {
		for(Item i: items){
			if(i.getItemName().equals(itemName)){
				return true;
			}
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see edu.bu.grocerylist.dao.IGroceryListItemDao#doesCategoryExist(java.lang.String)
	 */
	public boolean doesCategoryExist(String categoryName) {
		for(Category c: categories){
			if(c.getName().equals(categoryName)){
				return true;
			}
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see edu.bu.grocerylist.dao.IGroceryListItemDao#getCategoryIdByName(java.lang.String)
	 */
	public Category getCategoryIdByName(String name) {
		for(Category c: categories){
			if(c.getName().equals(name)){
				return c;
			}
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see edu.bu.grocerylist.dao.IGroceryListItemDao#getItemIdByName(java.lang.String)
	 */
	public Item getItemIdByName(String name) {
		for(Item i: items){
		if(i.getItemName().equals(name)){
			return i;
		}
	}
		return null;
	}

	public GroceryListItem getGroceryListItemByPrimaryKey(GroceryListItem sample) {
		for(GroceryListItem cur : listItems){
			if(cur.getItemId() == sample.getItemId() && cur.getListId() == sample.getListId() && cur.getCategoryId() == sample.getCategoryId()){
				
				return cur;
			}
		}
		return null;
	}

	public void deleteAllGroceryListItemsFromList(GroceryList grocerylist) {
		// TODO Auto-generated method stub
		
	}

}
